﻿Public Class frmOperation

    Private lineid As String = ""
    Private stackno As Integer = 0
    Private stacknum As Integer = 0
    Private stackTopNum As Integer = 0
    Private lineindex As Integer = -1

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub


    Private Sub btnLine1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine1.Click
        Me.lbTitle.Text = "一号生产线生产垛数修改"
        Me.lineid = "1"
        Me.lineindex = 0
        setOptParam()
    End Sub

    Private Sub btnLine2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine2.Click
        Me.lbTitle.Text = "二号生产线生产垛数修改"
        Me.lineid = "2"
        Me.lineindex = 1
        setOptParam()
    End Sub

    Private Sub btnLine3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine3.Click
        Me.lbTitle.Text = "三号生产线生产垛数修改"
        Me.lineid = "3"
        Me.lineindex = 2
        setOptParam()
    End Sub

    Private Sub btnLine4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLine4.Click
        Me.lbTitle.Text = "四号生产线生产垛数修改"
        Me.lineid = "4"
        Me.lineindex = 3
        setOptParam()
    End Sub

    Private Sub btnConfirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirm.Click

        Dim flag As Integer = 0
        Dim lineindex As Integer = CInt(Me.lineid) - 1
        '更新当前包数
        If (stacknum <> Me.nudStackNum.Value) Then
            If (updatePLCDate(prodsum(lineindex), Me.nudProdSum.Value)) Then '设置产品总数
                linedata(lineindex).producesum = Me.nudProdSum.Value
                If updatePLCDate(prodsumbak(lineindex), Me.nudProdSum.Value) = False Then  '设置产品总数临时数据
                    flag = 2
                End If
            Else
                flag = 1
            End If
        End If

        If (flag <> 0) Then
            MsgBox("出错了", MsgBoxTitle)
        Else
            MsgBox("修改包数操作成功")
        End If
    End Sub

    Private Sub setOptParam()
        Me.nudStackNo.Value = linedata(lineindex).stackno
        Me.nudStackNum.Value = linedata(lineindex).stacknum
        Me.nudProdSum.Value = linedata(lineindex).producesum
    End Sub

    Private Sub frmOperation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.lbTitle.Text = "一号生产线生产垛数修改"
        Me.lineid = "1"
        Me.lineindex = 0
        setOptParam()
    End Sub

    Private Sub btnAddStackNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddStackNum.Click
        Me.nudStackNum.Value = Me.nudStackNum.Value + 1
        Me.nudProdSum.Value = Me.nudProdSum.Value + 1
    End Sub

    Private Sub btnSubStackNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubStackNum.Click
        Me.nudStackNum.Value = Me.nudStackNum.Value - 1
        Me.nudProdSum.Value = Me.nudProdSum.Value - 1
    End Sub

    Private Sub btnTempClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTempClear.Click
        '临时清垛
        If MsgBox("确认要进行临时清垛？", MsgBoxStyle.YesNo, MsgBoxTitle) = MsgBoxResult.No Then
            Exit Sub
        End If
        Dim flag As Integer = 0
        Dim lineindex As Integer = CInt(Me.lineid) - 1
        Dim temp As Integer = Me.nudStackNum.Value
        Me.nudStackNum.Value = getPLCDate(drsnum(lineindex))
        Dim prodtemp As Integer = getPLCDate(prodsumtemp(lineindex)) '获得当前产品总数的临时数据
        If (prodtemp = -1) Then
            flag = 1
        End If

        '更新当前包数
        If (updatePLCDate(prodsumtemp(lineindex), Me.nudStackNum.Value - temp + prodtemp) = False) Then '设置产品总数临时数据，但是产品总数不予修改
            flag = 2
        End If

        If (flag <> 0) Then
            MsgBox("出错了", MsgBoxTitle)
        Else
            MsgBox("清垛操作成功")
        End If
    End Sub
End Class